package com.qtt.app.service.impl;


import com.qtt.app.entity.*;
import com.qtt.app.entity.ReturnInfo.ReturnResearch;
import com.qtt.app.entity.ReturnInfo.ReturnTopCourses;
import com.qtt.app.entity.ReturnInfo.ReturnTotalCourseInfo;
import com.qtt.app.entity.ReturnInfo.StudyCourse.*;
import com.qtt.app.mapper.*;
import com.qtt.app.service.AppCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.qtt.app.utils.DateUtil.getOfflineCourseAlreadyStudyNum;
import static com.qtt.app.utils.DateUtil.isNowTimeInDuration;
import static com.qtt.app.utils.JSONUtils2.jsonStrToList;
import static com.qtt.app.utils.JsonUtils3.stringToList;

@Service
public class AppCourseServiceImpl implements AppCourseService {
    @Autowired
    CourseClassMapper courseClassMapper;
    @Autowired
    CourseMessageMapper courseMessageMapper;
    @Autowired
    ExactCourseMapper exactCourseMapper;
    @Autowired
    GeneralCourseMapper generalCourseMapper;
    @Autowired
    AppCourseMapper appCourseMapper;
    @Autowired
    AppInstitutionMapper appInstitutionMapper;
    @Autowired
    TeachersMapper teachersMapper;
    @Autowired
    AppStudyHistoryMapper appStudyHistoryMapper;

    @Override
    public List<AppCourse> getAllAppCourse() {
        List<AppCourse> entities = appCourseMapper.getAll();
        return entities;
    }

    @Override
    public List<CourseClass> getAllCourseClass() {
        List<CourseClass> entities = courseClassMapper.getAll();
        return entities;
    }

    @Override
    public List<ExactCourse> getAllExactCourse() {
        List<ExactCourse> entities = exactCourseMapper.getAll();
        return entities;
    }

    @Override
    public List<GeneralCourse> getAllGeneralCourse() {
        List<GeneralCourse> entities = generalCourseMapper.getAll();
        return entities;
    }

    //查看所有课程
    @Override
    public List<ReturnTotalCourseInfo> getAllAppCourseInfo() {
        List<ReturnTotalCourseInfo> returnTotalCourseInfoEntities = new ArrayList<ReturnTotalCourseInfo>();
        //获取到我自己拓展创建的appcourse表，接下来遍历它。
        List<AppCourse> appCourseEntities = appCourseMapper.getAll();
        for(int i = 0 ; i < appCourseEntities.size() ; i++) {
            System.out.println(appCourseEntities.get(i));
            AppCourse appCourseEntity ;//编译器提示=new AppCourse()是多余的。
            ReturnTotalCourseInfo returnTotalCourseInfoEntity = new ReturnTotalCourseInfo();
            //总共要整合四张表的字段数据到AppCourseInfo，第一步：整合appCourse的数据过来,该表含有诸多外键，并添加了几个原表没有的字段
            appCourseEntity = appCourseEntities.get(i);
            returnTotalCourseInfoEntity.setAppCourseId( appCourseEntity.getAppcourseId());
            returnTotalCourseInfoEntity.setCourseId( appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setCourseInstitutionId(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setGeneralId( appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setCourseProgress(appCourseEntity.getCourseProgress());
            returnTotalCourseInfoEntity.setCourseType(appCourseEntity.getCourseType());

            //第二步：整合general_course的数据过来
            GeneralCourse generalCourseEntity = generalCourseMapper.selectByPrimaryKey(appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setPrice(generalCourseEntity.getPrice());
            returnTotalCourseInfoEntity.setIntroduction(generalCourseEntity.getIntroduction());
            returnTotalCourseInfoEntity.setCategory(generalCourseEntity.getCategory());
            returnTotalCourseInfoEntity.setService(generalCourseEntity.getServeice());
            returnTotalCourseInfoEntity.setMainPicture(generalCourseEntity.getMainPicture());
            returnTotalCourseInfoEntity.setOtherPicture(generalCourseEntity.getOtherPicture());
            returnTotalCourseInfoEntity.setCourseOrder(generalCourseEntity.getCourseOrder());
            returnTotalCourseInfoEntity.setVideo(generalCourseEntity.getVideo());
            returnTotalCourseInfoEntity.setTeacherId(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTitle(generalCourseEntity.getTitle());
            returnTotalCourseInfoEntity.setState(generalCourseEntity.getState());
            //status不知道干嘛的，没有设置。该表共13字段，除去id和status此处正好有11个

            //第三步：整合exact_course的数据过来，该表中以course_id为主键，虽然他名字叫id实际含义是courseid.
            ExactCourse exactCourseEntity = exactCourseMapper.getExactCourseByCourseId(appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setPeriod(exactCourseEntity.getPeriod());
            returnTotalCourseInfoEntity.setPlace(exactCourseEntity.getPlace());
            returnTotalCourseInfoEntity.setOpeningNum(exactCourseEntity.getOpeningNum());
            returnTotalCourseInfoEntity.setBuyNum(exactCourseEntity.getBuyNum());
            returnTotalCourseInfoEntity.setOpeningTime(exactCourseEntity.getOpeningTime());
            returnTotalCourseInfoEntity.setStaffId(exactCourseEntity.getStaffId());
            //该表共9个字段，除去id即course_id,status不知道干嘛的，general_id,刚好六个。


            //第四步，整合course_class表格,该表有course_id作为外键，可以此作为查询条件。因为course_id并非主键所以需要自定义方法。
            String course_id = appCourseEntity.getGeneralId();//很离谱的是，courseclass表中course_id字段对应的居然是general_id;
            CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(course_id);
            returnTotalCourseInfoEntity.setAddress(courseClassEntity.getAddress());
            returnTotalCourseInfoEntity.setClassHour(courseClassEntity.getClassHour());
            returnTotalCourseInfoEntity.setDuration(courseClassEntity.getDuration());
            //该表共5个字段，除去id和course_id,共有3个字段。

            //第五步，整合app_institution表.institution_id为主键，可直接采用自带的主键查找。
            AppInstitution appInstitutionEntity = appInstitutionMapper.selectByPrimaryKey(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setInstitutionName(appInstitutionEntity.getInstitutionName());
            returnTotalCourseInfoEntity.setLogoPicture(appInstitutionEntity.getLogoPicture());
            returnTotalCourseInfoEntity.setInstitutionIntroduction(appInstitutionEntity.getIntroduction());


            //第六步，整合teacher表。老师的id放在原general_course表中，需要从那个实体中取出。该teacher_id为主键，可直接采用自带的主键查找。
            Teachers teachersEntity = teachersMapper.selectByPrimaryKey(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTeacherName(teachersEntity.getRealName());
            returnTotalCourseInfoEntity.setTeacherPicture(teachersEntity.getHead());
            returnTotalCourseInfoEntity.setTeacherGrade(teachersEntity.getGrade());


            //最后，添加该entity到list集合中
            returnTotalCourseInfoEntities.add(returnTotalCourseInfoEntity);
        }
        return returnTotalCourseInfoEntities;
    }

    //查看某机构的所有课程
    @Override
    public List<ReturnTotalCourseInfo> getInstitutionCourseByInstId(Integer institutionId) {
        List<ReturnTotalCourseInfo> returnTotalCourseInfoEntities = new ArrayList<ReturnTotalCourseInfo>();
        //获取到我自己拓展创建的appcourse表，接下来遍历它。
        List<AppCourse> appCourseEntities = appCourseMapper.getInstitutionCourse(institutionId);
        for(int i = 0 ; i < appCourseEntities.size() ; i++) {
            System.out.println(appCourseEntities.get(i));
            AppCourse appCourseEntity ;//编译器提示=new AppCourse()是多余的。
            ReturnTotalCourseInfo returnTotalCourseInfoEntity = new ReturnTotalCourseInfo();
            //总共要整合四张表的字段数据到AppCourseInfo，第一步：整合appCourse的数据过来,该表含有诸多外键，并添加了几个原表没有的字段
            appCourseEntity = appCourseEntities.get(i);
            returnTotalCourseInfoEntity.setAppCourseId( appCourseEntity.getAppcourseId());
            returnTotalCourseInfoEntity.setCourseId( appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setCourseInstitutionId(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setGeneralId( appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setCourseProgress(appCourseEntity.getCourseProgress());
            returnTotalCourseInfoEntity.setCourseType(appCourseEntity.getCourseType());

            //第二步：整合general_course的数据过来
            GeneralCourse generalCourseEntity = generalCourseMapper.selectByPrimaryKey(appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setPrice(generalCourseEntity.getPrice());
            returnTotalCourseInfoEntity.setIntroduction(generalCourseEntity.getIntroduction());
            returnTotalCourseInfoEntity.setCategory(generalCourseEntity.getCategory());
            returnTotalCourseInfoEntity.setService(generalCourseEntity.getServeice());
            returnTotalCourseInfoEntity.setMainPicture(generalCourseEntity.getMainPicture());
            returnTotalCourseInfoEntity.setOtherPicture(generalCourseEntity.getOtherPicture());
            returnTotalCourseInfoEntity.setCourseOrder(generalCourseEntity.getCourseOrder());
            returnTotalCourseInfoEntity.setVideo(generalCourseEntity.getVideo());
            returnTotalCourseInfoEntity.setTeacherId(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTitle(generalCourseEntity.getTitle());
            returnTotalCourseInfoEntity.setState(generalCourseEntity.getState());
            //status不知道干嘛的，没有设置。该表共13字段，除去id和status此处正好有11个。

            //第三步：整合exact_course的数据过来，该表中以course_id为主键，虽然他名字叫id实际含义是courseid.
            ExactCourse exactCourseEntity = exactCourseMapper.getExactCourseByCourseId(appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setPeriod(exactCourseEntity.getPeriod());
            returnTotalCourseInfoEntity.setPlace(exactCourseEntity.getPlace());
            returnTotalCourseInfoEntity.setOpeningNum(exactCourseEntity.getOpeningNum());
            returnTotalCourseInfoEntity.setBuyNum(exactCourseEntity.getBuyNum());
            returnTotalCourseInfoEntity.setOpeningTime(exactCourseEntity.getOpeningTime());
            returnTotalCourseInfoEntity.setStaffId(exactCourseEntity.getStaffId());
            //该表共9个字段，除去id即course_id,status不知道干嘛的，general_id,刚好六个。


            //第四步，整合course_class表格,该表有course_id作为外键，可以此作为查询条件。因为course_id并非主键所以需要自定义方法。
            String course_id = appCourseEntity.getGeneralId();//很离谱的是，courseclass表中course_id字段对应的居然是general_id;
            CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(course_id);
            returnTotalCourseInfoEntity.setAddress(courseClassEntity.getAddress());
            returnTotalCourseInfoEntity.setClassHour(courseClassEntity.getClassHour());
            returnTotalCourseInfoEntity.setDuration(courseClassEntity.getDuration());
            //该表共5个字段，除去id和course_id,共有3个字段。

            //第五步，整合app_institution表.institution_id为主键，可直接采用自带的主键查找。
            AppInstitution appInstitutionEntity = appInstitutionMapper.selectByPrimaryKey(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setInstitutionName(appInstitutionEntity.getInstitutionName());
            returnTotalCourseInfoEntity.setLogoPicture(appInstitutionEntity.getLogoPicture());
            returnTotalCourseInfoEntity.setInstitutionIntroduction(appInstitutionEntity.getIntroduction());


            //第六步，整合teacher表。老师的id放在原general_course表中，需要从那个实体中取出。该teacher_id为主键，可直接采用自带的主键查找。
            Teachers teachersEntity = teachersMapper.selectByPrimaryKey(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTeacherName(teachersEntity.getRealName());
            returnTotalCourseInfoEntity.setTeacherPicture(teachersEntity.getHead());
            returnTotalCourseInfoEntity.setTeacherGrade(teachersEntity.getGrade());


            //最后，添加该entity到list集合中
            returnTotalCourseInfoEntities.add(returnTotalCourseInfoEntity);
        }
        return returnTotalCourseInfoEntities;
    }


    //添加一门课程记录
    @Override
    public int addAppCourse(AppCourseWithBLOBs appCourseEntity) {

        int flag = appCourseMapper.insert(appCourseEntity);
        //返回插入数据后的记录id
        int id;
        if(flag == 1){
            id = appCourseMapper.getLastId();
        }
        else{
            id = 0;
        }
        return id;
    }

    @Override
    public ReturnTotalCourseInfo getCourseById(Integer appcourseId) {
        ReturnTotalCourseInfo returnTotalCourseInfoEntities = new ReturnTotalCourseInfo();
        //获取到我自己拓展创建的appcourse表，接下来遍历它。
        AppCourse appCourseEntity = appCourseMapper.selectByPrimaryKey(appcourseId);
        ReturnTotalCourseInfo returnTotalCourseInfoEntity = new ReturnTotalCourseInfo();
        if(appCourseEntity!=null) {
            //总共要整合四张表的字段数据到AppCourseInfo，第一步：整合appCourse的数据过来,该表含有诸多外键，并添加了几个原表没有的字段
            returnTotalCourseInfoEntity.setAppCourseId(appCourseEntity.getAppcourseId());
            returnTotalCourseInfoEntity.setCourseId(appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setCourseInstitutionId(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setGeneralId(appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setCourseProgress(appCourseEntity.getCourseProgress());
            returnTotalCourseInfoEntity.setCourseType(appCourseEntity.getCourseType());

            //第二步：整合general_course的数据过来
            GeneralCourse generalCourseEntity = generalCourseMapper.selectByPrimaryKey(appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setPrice(generalCourseEntity.getPrice());
            returnTotalCourseInfoEntity.setIntroduction(generalCourseEntity.getIntroduction());
            returnTotalCourseInfoEntity.setCategory(generalCourseEntity.getCategory());
            returnTotalCourseInfoEntity.setService(generalCourseEntity.getServeice());
            returnTotalCourseInfoEntity.setMainPicture(generalCourseEntity.getMainPicture());
            returnTotalCourseInfoEntity.setOtherPicture(generalCourseEntity.getOtherPicture());
            returnTotalCourseInfoEntity.setCourseOrder(generalCourseEntity.getCourseOrder());
            returnTotalCourseInfoEntity.setVideo(generalCourseEntity.getVideo());
            returnTotalCourseInfoEntity.setTeacherId(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTitle(generalCourseEntity.getTitle());
            returnTotalCourseInfoEntity.setState(generalCourseEntity.getState());
            //status不知道干嘛的，没有设置。该表共13字段，除去id和status此处正好有11个。

            //第三步：整合exact_course的数据过来，该表中以course_id为主键，虽然他名字叫id实际含义是courseid.
            ExactCourse exactCourseEntity = exactCourseMapper.getExactCourseByCourseId(appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setPeriod(exactCourseEntity.getPeriod());
            returnTotalCourseInfoEntity.setPlace(exactCourseEntity.getPlace());
            returnTotalCourseInfoEntity.setOpeningNum(exactCourseEntity.getOpeningNum());
            returnTotalCourseInfoEntity.setBuyNum(exactCourseEntity.getBuyNum());
            returnTotalCourseInfoEntity.setOpeningTime(exactCourseEntity.getOpeningTime());
            returnTotalCourseInfoEntity.setStaffId(exactCourseEntity.getStaffId());
            //该表共9个字段，除去id即course_id,status不知道干嘛的，general_id,刚好六个。


            //第四步，整合course_class表格,该表有course_id作为外键，可以此作为查询条件。因为course_id并非主键所以需要自定义方法。
            String course_id = appCourseEntity.getGeneralId();//很离谱的是，courseclass表中course_id字段对应的居然是general_id;
            CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(course_id);
            returnTotalCourseInfoEntity.setAddress(courseClassEntity.getAddress());
            returnTotalCourseInfoEntity.setClassHour(courseClassEntity.getClassHour());
            returnTotalCourseInfoEntity.setDuration(courseClassEntity.getDuration());
            //该表共5个字段，除去id和course_id,共有3个字段

            //第五步，整合app_institution表.institution_id为主键，可直接采用自带的主键查找。
            AppInstitution appInstitutionEntity = appInstitutionMapper.selectByPrimaryKey(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setInstitutionName(appInstitutionEntity.getInstitutionName());
            returnTotalCourseInfoEntity.setLogoPicture(appInstitutionEntity.getLogoPicture());
            returnTotalCourseInfoEntity.setInstitutionIntroduction(appInstitutionEntity.getIntroduction());


            //第六步，整合teacher表。老师的id放在原general_course表中，需要从那个实体中取出。该teacher_id为主键，可直接采用自带的主键查找。
            Teachers teachersEntity = teachersMapper.selectByPrimaryKey(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTeacherName(teachersEntity.getRealName());
            returnTotalCourseInfoEntity.setTeacherPicture(teachersEntity.getHead());
            returnTotalCourseInfoEntity.setTeacherGrade(teachersEntity.getGrade());

        }

        return returnTotalCourseInfoEntity;





    }

    @Override
    public List<GeneralCourse> getTeacherCourseByTeacherId(Integer teacherId) {
        List<GeneralCourse> generalCoursesEntities = generalCourseMapper.getCourseByTeacherId(teacherId);

        return generalCoursesEntities;
    }

    @Override
    public List<ReturnTotalCourseInfo> getCoursesByType(String courseType) {
        List<ReturnTotalCourseInfo> returnTotalCourseInfoEntities = new ArrayList<ReturnTotalCourseInfo>();
        //获取到我自己拓展创建的appcourse表，接下来遍历它。
        List<AppCourse> appCourseEntities = appCourseMapper.getCoursesByType(courseType);
        for(int i = 0 ; i < appCourseEntities.size() ; i++) {
            System.out.println(appCourseEntities.get(i));
            AppCourse appCourseEntity ;//编译器提示=new AppCourse()是多余的。
            ReturnTotalCourseInfo returnTotalCourseInfoEntity = new ReturnTotalCourseInfo();
            //总共要整合四张表的字段数据到AppCourseInfo，第一步：整合appCourse的数据过来,该表含有诸多外键，并添加了几个原表没有的字段
            appCourseEntity = appCourseEntities.get(i);
            returnTotalCourseInfoEntity.setAppCourseId( appCourseEntity.getAppcourseId());
            returnTotalCourseInfoEntity.setCourseId( appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setCourseInstitutionId(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setGeneralId( appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setCourseProgress(appCourseEntity.getCourseProgress());
            returnTotalCourseInfoEntity.setCourseType(appCourseEntity.getCourseType());

            //第二步：整合general_course的数据过来
            GeneralCourse generalCourseEntity = generalCourseMapper.selectByPrimaryKey(appCourseEntity.getGeneralId());
            returnTotalCourseInfoEntity.setPrice(generalCourseEntity.getPrice());
            returnTotalCourseInfoEntity.setIntroduction(generalCourseEntity.getIntroduction());
            returnTotalCourseInfoEntity.setCategory(generalCourseEntity.getCategory());
            returnTotalCourseInfoEntity.setService(generalCourseEntity.getServeice());
            returnTotalCourseInfoEntity.setMainPicture(generalCourseEntity.getMainPicture());
            returnTotalCourseInfoEntity.setOtherPicture(generalCourseEntity.getOtherPicture());
            returnTotalCourseInfoEntity.setCourseOrder(generalCourseEntity.getCourseOrder());
            returnTotalCourseInfoEntity.setVideo(generalCourseEntity.getVideo());
            returnTotalCourseInfoEntity.setTeacherId(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTitle(generalCourseEntity.getTitle());
            returnTotalCourseInfoEntity.setState(generalCourseEntity.getState());
            //status不知道干嘛的，没有设置。该表共13字段，除去id和status此处正好有11个。

            //第三步：整合exact_course的数据过来，该表中以course_id为主键，虽然他名字叫id实际含义是courseid.
            ExactCourse exactCourseEntity = exactCourseMapper.getExactCourseByCourseId(appCourseEntity.getCourseId());
            returnTotalCourseInfoEntity.setPeriod(exactCourseEntity.getPeriod());
            returnTotalCourseInfoEntity.setPlace(exactCourseEntity.getPlace());
            returnTotalCourseInfoEntity.setOpeningNum(exactCourseEntity.getOpeningNum());
            returnTotalCourseInfoEntity.setBuyNum(exactCourseEntity.getBuyNum());
            returnTotalCourseInfoEntity.setOpeningTime(exactCourseEntity.getOpeningTime());
            returnTotalCourseInfoEntity.setStaffId(exactCourseEntity.getStaffId());
            //该表共9个字段，除去id即course_id,status不知道干嘛的，general_id,刚好六个。


            //第四步，整合course_class表格,该表有course_id作为外键，可以此作为查询条件。因为course_id并非主键所以需要自定义方法。
            String course_id = appCourseEntity.getGeneralId();//很离谱的是，courseclass表中course_id字段对应的居然是general_id;
            CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(course_id);
            returnTotalCourseInfoEntity.setAddress(courseClassEntity.getAddress());
            returnTotalCourseInfoEntity.setClassHour(courseClassEntity.getClassHour());
            returnTotalCourseInfoEntity.setDuration(courseClassEntity.getDuration());
            //该表共5个字段，除去id和course_id,共有3个字段。

            //第五步，整合app_institution表.institution_id为主键，可直接采用自带的主键查找。
            AppInstitution appInstitutionEntity = appInstitutionMapper.selectByPrimaryKey(appCourseEntity.getInstitutionId());
            returnTotalCourseInfoEntity.setInstitutionName(appInstitutionEntity.getInstitutionName());
            returnTotalCourseInfoEntity.setLogoPicture(appInstitutionEntity.getLogoPicture());
            returnTotalCourseInfoEntity.setInstitutionIntroduction(appInstitutionEntity.getIntroduction());

            //第六步，整合teacher表。老师的id放在原general_course表中，需要从那个实体中取出。该teacher_id为主键，可直接采用自带的主键查找。
            Teachers teachersEntity = teachersMapper.selectByPrimaryKey(generalCourseEntity.getTeacherId());
            returnTotalCourseInfoEntity.setTeacherName(teachersEntity.getRealName());
            returnTotalCourseInfoEntity.setTeacherPicture(teachersEntity.getHead());
            returnTotalCourseInfoEntity.setTeacherGrade(teachersEntity.getGrade());

            //最后，添加该entity到list集合中
            returnTotalCourseInfoEntities.add(returnTotalCourseInfoEntity);
        }
        return returnTotalCourseInfoEntities;
    }

    @Override
    public GeneralCourse getLastEntity() {
        GeneralCourse generalCourseEntity = generalCourseMapper.getLastEntity();
        return generalCourseEntity;
    }

    @Override
    public GeneralCourse getGeneralCourseByTitle(String title) {
        GeneralCourse generalCourseEntity = generalCourseMapper.getGeneralCourseByTitle(title);
        return generalCourseEntity;
    }

    @Override
    public List<ReturnResearch> searchCourses(String keyword) {
        String keyword2 = "%"+keyword+"%";
        List<ReturnResearch> generalCourseEntities = generalCourseMapper.searchCourse(keyword2);
        return generalCourseEntities;
    }

    @Override
    public List<ReturnTotalCourseInfo> getOnlineCourses(int flag,Long userId) {
        List<ReturnTotalCourseInfo> returnTotalCourseInfoEntities = new ArrayList<ReturnTotalCourseInfo>();

        if(flag==1) {
            returnTotalCourseInfoEntities = appCourseMapper.getOnlineCoursesByUserId(userId);
            return returnTotalCourseInfoEntities;
        }
        if(flag==2){
            returnTotalCourseInfoEntities = appCourseMapper.getOfflineCoursesByUserId(userId);
            return returnTotalCourseInfoEntities;
        }
        return returnTotalCourseInfoEntities;

    }

    @Override
    public ReturnStudyCourseDetails getStudyCourseDetails(String generalId) {
        ReturnStudyCourseDetails returnStudyCourseDetailsEntity = new ReturnStudyCourseDetails();
        //获取到app_course表的主键id，并拿取其中的字段
        Integer id = appCourseMapper.getIdByGeneralId(generalId );
        AppCourseWithBLOBs appCourseWithBLOBsEntity =appCourseMapper.selectByPrimaryKey(id);
        String noticeJsonStr = appCourseWithBLOBsEntity.getNoticeList();
        String chapterJsonStr = appCourseWithBLOBsEntity.getChapterList();
        String sectionJsonStr = appCourseWithBLOBsEntity.getSectionList();
        String resourceJsonStr = appCourseWithBLOBsEntity.getResourceList();
        System.out.println("get里面，chapterJsonStr= "+chapterJsonStr);

        Class clazz_Notice = Notice.class;
        List<Notice> noticeList =jsonStrToList(noticeJsonStr,clazz_Notice);
        System.out.println("get里面，noticeList= "+noticeList);

        Class clazz_Chapter = Chapter.class;
        List<Chapter> chapterList =jsonStrToList(chapterJsonStr,clazz_Chapter);
        System.out.println("get里面，chapterList= "+chapterList);

        Class clazz_Section = Section.class;
        List<Section> sectionList =jsonStrToList(sectionJsonStr,clazz_Section);

        Class clazz_Resource = Resource.class;
        List<Resource> resourceList =jsonStrToList(resourceJsonStr,clazz_Resource);

        //  返回体赋值
        returnStudyCourseDetailsEntity.setNoticeList(noticeList);
        returnStudyCourseDetailsEntity.setChapterList(chapterList);
        returnStudyCourseDetailsEntity.setSectionList(sectionList);
        returnStudyCourseDetailsEntity.setResourceList(resourceList);

        //找到评论表并返回
        returnStudyCourseDetailsEntity.setCommentList(null);

        return returnStudyCourseDetailsEntity;

    }

    @Override
    public List<Video> getStudyCourseVideos(String generalId) {
        //创建返回体列表
        List<Video> videoEntities = new ArrayList<>();

        //获取到app_course表的主键id，并拿取其中的字段
        Integer id = appCourseMapper.getIdByGeneralId(generalId);
        AppCourseWithBLOBs appCourseWithBLOBsEntity =appCourseMapper.selectByPrimaryKey(id);
        String chapterJsonStr = appCourseWithBLOBsEntity.getChapterList();
        System.out.println("chapterStr="+chapterJsonStr);
        //使用json工具类3的方法将json字符串转换为列表
        Class clazz_ReturnVideo = Video.class;
        List<Video> changeVideoList =stringToList(chapterJsonStr,clazz_ReturnVideo);
        System.out.println("list="+changeVideoList);
        return changeVideoList;
    }

    @Override
    public List<Notice> getStudyCourseNotice(String generalId) {
        //获取到app_course表的主键id，并拿取其中的字段
        Integer id = appCourseMapper.getIdByGeneralId(generalId);
        AppCourseWithBLOBs appCourseWithBLOBsEntity =appCourseMapper.selectByPrimaryKey(id);
        String noticeJsonStr = appCourseWithBLOBsEntity.getNoticeList();
//        System.out.println("chapterStr="+noticeJsonStr);
        //使用json工具类3的方法将json字符串转换为列表
        Class clazz_ReturnNotice = Notice.class;
        List<Notice> noticeList =stringToList(noticeJsonStr,clazz_ReturnNotice);
        System.out.println("list="+noticeList);
        return noticeList;
    }

    @Override
    public List<Resource> getStudyCourseResource(String generalId) {

        //获取到app_course表的主键id，并拿取其中的字段
        Integer id = appCourseMapper.getIdByGeneralId(generalId);
        AppCourseWithBLOBs appCourseWithBLOBsEntity =appCourseMapper.selectByPrimaryKey(id);
        String resourceJsonStr = appCourseWithBLOBsEntity.getResourceList();
        //使用json工具类3的方法将json字符串转换为列表
        Class clazz_ReturnResource = Resource.class;
        List<Resource> resourceList = stringToList(resourceJsonStr,clazz_ReturnResource);
        System.out.println("list="+resourceList);

        return resourceList;
    }

    @Override
    public List<ReturnMyOnlineCourse> getMyOnlineCourses(int flag, Long userId) {
        List<ReturnMyOnlineCourse> returnTotalCourseInfoEntities = new ArrayList<>();
        //1为线上课
        if(flag==1) {
            returnTotalCourseInfoEntities = appCourseMapper.getMyOnlineCoursesByUserId(userId);
            //拿取duration字段，并判断课程状态，是否开始、进行中、已结束
            for(int i=0;i<returnTotalCourseInfoEntities.size();i++){
                ReturnMyOnlineCourse returnMyOnlineCourseEntity = returnTotalCourseInfoEntities.get(i);
                String durantionStr = returnMyOnlineCourseEntity.getCourseDuration();
                Map<String,Object> map = isNowTimeInDuration(durantionStr);
                System.out.println("map="+map);
                Integer timeFlag =  (Integer)map.get("timeFlag");
                String courseTime = (String)map.get("courseTime");
                returnMyOnlineCourseEntity.setCourseDuration(courseTime);
                returnMyOnlineCourseEntity.setCourseState(timeFlag);

                //判断当前属于第几课时，线上课需要通过观看历史表来判断，总课时为class_hour,
                // 已学习课时则为该课程下的视频观看记录数量，需要用户id、课程id、订单id共同确认。以免同一课程不同订单造成的重复。
                String generalId = returnMyOnlineCourseEntity.getGeneralId();
                String orderId = returnMyOnlineCourseEntity.getOrderId();
                CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(generalId);
                Integer totalClassNum = courseClassEntity.getClassHour();
                Integer alreadyStudyNum = appStudyHistoryMapper.getMyAlreadyStudyNum(userId,generalId,orderId);
                returnMyOnlineCourseEntity.setAlreadyStudyNum(alreadyStudyNum);
                returnMyOnlineCourseEntity.setTotalClassNum(totalClassNum);
            }
            return returnTotalCourseInfoEntities;
        }
        if(flag==2){
            returnTotalCourseInfoEntities = appCourseMapper.getMyOfflineCoursesByUserId(userId);
            //拿取duration字段，并判断课程状态，是否开始、进行中、已结束
            for(int i=0;i<returnTotalCourseInfoEntities.size();i++){
                ReturnMyOnlineCourse returnMyOnlineCourseEntity = returnTotalCourseInfoEntities.get(i);
                String durationStr = returnMyOnlineCourseEntity.getCourseDuration();
                Map<String,Object> map = isNowTimeInDuration(durationStr);
                System.out.println("map="+map);
                Integer timeFlag =  (Integer)map.get("timeFlag");
                String courseTime = (String)map.get("courseTime");
                returnMyOnlineCourseEntity.setCourseDuration(courseTime);
                returnMyOnlineCourseEntity.setCourseState(timeFlag);

                //判断当前属于第几课时，线下课只需要通过duration来判断即可，可写一个函数判断
                String[] durationList = durationStr.split(",");
                Integer totalClassNum = durationList.length;
                Integer alreadyStudyNum = getOfflineCourseAlreadyStudyNum(durationStr);

                returnMyOnlineCourseEntity.setAlreadyStudyNum(alreadyStudyNum);
                returnMyOnlineCourseEntity.setTotalClassNum(totalClassNum);
            }
            return returnTotalCourseInfoEntities;
        }
        return returnTotalCourseInfoEntities;

    }

    @Override
    public List<ReturnMyOnlineCourse> getMyCourseAll(Long userId) {
        List<ReturnMyOnlineCourse> returnTotalCourseInfoEntities = new ArrayList<>();

        returnTotalCourseInfoEntities = appCourseMapper.getMyCourseAll(userId);
        //拿取duration字段，并判断课程状态，是否开始、进行中、已结束
        for(int i=0;i<returnTotalCourseInfoEntities.size();i++){
            ReturnMyOnlineCourse returnMyOnlineCourseEntity = returnTotalCourseInfoEntities.get(i);
            String durantionStr = returnMyOnlineCourseEntity.getCourseDuration();
            Map<String,Object> map = isNowTimeInDuration(durantionStr);
            System.out.println("map="+map);
            Integer timeFlag =  (Integer)map.get("timeFlag");
            String courseTime = (String)map.get("courseTime");
            returnMyOnlineCourseEntity.setCourseDuration(courseTime);
            returnMyOnlineCourseEntity.setCourseState(timeFlag);
        }

        return returnTotalCourseInfoEntities;

    }

    @Override
    public List<ReturnTopCourses> getTopCourses() {
        List<ReturnTopCourses> returnTopCoursesEntities= appCourseMapper.getTopCourses();
        return returnTopCoursesEntities;
    }

    @Override
    public NoticeOffline getOfflineCourseNotice(String generalId) {
        NoticeOffline noticeOfflineEntities = appCourseMapper.getOfflineCourseNotice(generalId);
        noticeOfflineEntities.setSignInMsg("第xx次线下课程签到");
        noticeOfflineEntities.setSignInQrcode("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E4%BA%8C%E7%BB%B4%E7%A0%81&hs=2&pn=0&spn=0&di=171630&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=528676435%2C1806536374&os=1344762543%2C168236172&simid=0%2C0&adpicid=0&lpn=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=11&oriquery=%E4%BA%8C%E7%BB%B4%E7%A0%81&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fn1.hdfimg.com%2Fg8%2FM07%2FF3%2F55%2FtoYBAGBN-8GAFhCxAAAD9f6VmJ4867.png%3F_ms_%3D0f94%26refer%3Dhttp%3A%2F%2Fn1.hdfimg.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1622193778%26t%3D1af9f5ad4658d56da586fe7a11b4dcbb&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Biw51u_z%26e3Bv54AzdH3Fzi7wg3tw27wg1twgAzdH3Fxtg23twg16_l8ac80acbl_z%26e3Bip4&gsm=1&islist=&querylist=");
        return noticeOfflineEntities;
    }


}
